package com.ptbuy;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

import com.ptbuy.base.BaseApplication;
import com.ptbuy.pdfview.MyPDFView;

public class IndexActivity extends AppCompatActivity {

    //是否是第一次进来
    private boolean isFirstIn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);
        isFirstIn = BaseApplication.getSharedPreferences().getBoolean("isFirstIn", true);
        if (isFirstIn) {
            showClauseDialog();
        } else {
            redirct();
        }
    }

    private void redirct() {
        startActivity(new Intent(this, MainActivity.class));
        finish();
    }

    Dialog clauseDialog;

    private void showClauseDialog() {
        //显示dialog提示框
        View dialogView = LayoutInflater.from(this).
                inflate(R.layout.home_dialog_clause, null);
        clauseDialog = new android.app.AlertDialog.
                Builder(this).
                setView(dialogView).
                show();
        //设置点击弹出框以外不关闭
        clauseDialog.setCancelable(false);

        final WindowManager.LayoutParams params = clauseDialog.getWindow().getAttributes();
        //获取屏幕分辨率
        DisplayMetrics metrics = getApplicationContext().getResources().getDisplayMetrics();

        int width = metrics.widthPixels;
        int height = metrics.heightPixels;
        //设置对话框大小
        params.width = Math.round(width * 4 / 5);
//        params.height = Math.round(height * 3 / 4);
        clauseDialog.getWindow().setAttributes(params);
        clauseDialog.getWindow().setBackgroundDrawableResource(android.R.color.white);

        Intent intent = new Intent(this, MyPDFView.class);
        TextView mydeal = dialogView.findViewById(R.id.mydeal);//协议
        mydeal.setOnClickListener(v -> {
            intent.putExtra("title", "用户协议");
            intent.putExtra("fileName", "fwyx.pdf");
            startActivity(intent);
        });
        TextView myprivacy = dialogView.findViewById(R.id.myprivacy);//隐私
        myprivacy.setOnClickListener(v -> {
            intent.putExtra("title", "隐私政策");
            intent.putExtra("fileName", "yszc.pdf");
            startActivity(intent);
        });


        final TextView view = dialogView.findViewById(R.id.content_text);
        view.setMovementMethod(ScrollingMovementMethod.getInstance());

        final CheckBox mCheckBox = dialogView.findViewById(R.id.checkBox);
        final Button mButton = dialogView.findViewById(R.id.clause_submit);
        final Button mButtonCancel = dialogView.findViewById(R.id.clause_cancel);
        mCheckBox.setChecked(false);
        mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                //勾选监听事件
                if (isChecked) {
                    mCheckBox.setTextColor(getResources().getColor(R.color.colorAccent));
                    //确定按钮监听
                    mButton.setOnClickListener(new View.OnClickListener() {

                        @RequiresApi(api = Build.VERSION_CODES.O)
                        @Override
                        public void onClick(View v) {
                            if (mCheckBox.isChecked()) {
                                clauseDialog.dismiss();
                                SharedPreferences sharedPreferences = BaseApplication.getSharedPreferences();
                                SharedPreferences.Editor edit = sharedPreferences.edit();
                                edit.putBoolean("isFirstIn", false);
                                edit.apply();
                                runOnUiThread(IndexActivity.this::redirct);
                            }
                        }
                    });
                } else {
                    mCheckBox.setTextColor(getResources().getColor(R.color.text_gray_one));
                }
            }
        });

        //确定按钮监听
        mButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (!mCheckBox.isChecked()) {
                    Toast.makeText(IndexActivity.this, "请勾选协议", Toast.LENGTH_SHORT).show();
                }
            }
        });

        mButtonCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getFragmentManager().popBackStack();
                clauseDialog.dismiss();
                System.exit(0);
            }
        });
        clauseDialog.show();
    }
}